/**

 @Name：layuiAdmin 主页控制台
 @Author：贤心
 @Site：http://www.layui.com/admin/
 @License：GPL-2
    
 */


layui.define(function (exports) {

  /*
    下面通过 layui.use 分段加载不同的模块，实现不同区域的同时渲染，从而保证视图的快速呈现
  */


  //区块轮播切换
  layui.use(['admin', 'carousel'], function () {
    var $ = layui.$
      , admin = layui.admin
      , carousel = layui.carousel
      , element = layui.element
      , device = layui.device();

    //轮播切换
    $('.layadmin-carousel').each(function () {
      var othis = $(this);
      carousel.render({
        elem: this
        , width: '100%'
        , arrow: 'none'
        , interval: othis.data('interval')
        , autoplay: othis.data('autoplay') === true
        , trigger: (device.ios || device.android) ? 'click' : 'hover'
        , anim: othis.data('anim')
      });
    });

    element.render('progress');

  });

  //数据概览
  layui.use(['admin', 'carousel', 'echarts'], function () {
    var $ = layui.$
      , admin = layui.admin
      , carousel = layui.carousel
      , echarts = layui.echarts;

    var echartsApp = []
      , echartsAll = []
      , options = [
        //今日案件
        {
          
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: ['', '']
          },
          xAxis: [{
            type: 'category',
            boundaryGap: false,
            data: ['06:00', '06:30', '07:00', '07:30', '08:00', '08:30', '09:00', '09:30', '10:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30', '22:00', '22:30', '23:00', '23:30']
          }],
          yAxis: [{
            type: 'value'
          }],
          series: [{
            name: '新增案件',
            type: 'line',
            smooth: true,
            itemStyle: { normal: { areaStyle: { type: 'default' } } },
            data: [111, 222, 333, 444, 555, 666, 3333, 33333, 55555, 66666, 33333, 3333, 6666, 11888, 26666, 38888, 56666, 42222, 39999, 28888, 17777, 9666, 6555, 5555, 3333, 2222, 3111, 6999, 5888, 2777, 1666, 999, 888, 777]
          }, {
            name: '处理案件',
            type: 'line',
            smooth: true,
            itemStyle: { normal: { areaStyle: { type: 'default' } } },
            data: [11, 22, 33, 44, 55, 66, 333, 3333, 5555, 12666, 3333, 333, 666, 1188, 2666, 3888, 6666, 4222, 3999, 2888, 1777, 966, 655, 555, 333, 222, 311, 699, 588, 277, 166, 99, 88, 77]
          }]
        },

        

        //新增的用户量
        {
          tooltip: { //提示框
            trigger: 'axis',
            formatter: "{b}<br>新增的案件：{c}"
          },
          xAxis: [{ //X轴
            type: 'category',
            data: ['11-07', '11-08', '11-09', '11-10', '11-11', '11-12', '11-13']
          }],
          yAxis: [{  //Y轴
            type: 'value'
          }],
          series: [{ //内容
            type: 'line',
            data: [200, 300, 400, 610, 150, 270, 380],
          }]
        },
        //特殊人群统计
        {
          title: {
            text: '特殊人群统计',
            x: 'center',
            textStyle: {
              fontSize: 14
            }
          },
          tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
          },
          legend: {
            orient: 'vertical',
            x: 'left',
            data: ['刑满释放人员', '社区矫正人员', '吸毒人员', '艾滋病危人员', '肇事肇祸人员']
          },
          series: [{
            name: '特殊人群统计',
            type: 'pie',
            radius: '55%',
            center: ['50%', '50%'],
            data: [
              { value: 9052, name: '刑满释放人员' },
              { value: 1610, name: '社区矫正人员' },
              { value: 3200, name: '吸毒人员' },
              { value: 535, name: '艾滋病危人员' },
              { value: 1700, name: '肇事肇祸人员' }
            ]
          }]
        },
        //重点青少年统计
        {
          title: {
            text: '重点青少年统计',
            x: 'center',
            textStyle: {
              fontSize: 14
            }
          },
          tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
          },
          legend: {
            orient: 'vertical',
            x: 'left',
            data: ['闲散青少年', '有不良行为青少年', '流浪乞讨青年', '服刑人员青少年', '农村留守青少安']
          },
          series: [{
            name: '特殊人群统计',
            type: 'pie',
            radius: '55%',
            center: ['50%', '50%'],
            data: [
              { value: 4785, name: '闲散青少年' },
              { value: 2145, name: '有不良行为青少年' },
              { value: 452, name: '流浪乞讨青年' },
              { value: 1544, name: '服刑人员青少年' },
              { value: 3682, name: '农村留守青少安' }
            ]
          }]
        },
        // 4
        {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              label: {
                backgroundColor: '#6a7985'
              }
            }
          },
          legend: {
            data: ['中网格1', '中网格2', '中网格3', '中网格4', '中网格5']
          },
          toolbox: {
            feature: {
              saveAsImage: {}
            }
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              boundaryGap: false,
              data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '中网格1',
              type: 'line',
              stack: '总量',
              areaStyle: {},
              data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
              name: '中网格2',
              type: 'line',
              stack: '总量',
              areaStyle: {},
              data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
              name: '中网格3',
              type: 'line',
              stack: '总量',
              areaStyle: {},
              data: [150, 232, 201, 154, 190, 330, 410]
            },
            {
              name: '中网格4',
              type: 'line',
              stack: '总量',
              areaStyle: { normal: {} },
              data: [320, 332, 301, 334, 390, 330, 320]
            },
            {
              name: '中网格5',
              type: 'line',
              stack: '总量',
              label: {
                normal: {
                  show: true,
                  position: 'top'
                }
              },
              areaStyle: { normal: {} },
              data: [820, 932, 901, 934, 1290, 1330, 1320]
            }
          ]
        },
        // 5
        {
          title: {
            text: '世界人口总量',
            subtext: '数据来自网络'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          legend: {
            data: ['2011年', '2012年']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'value',
            boundaryGap: [0, 0.01]
          },
          yAxis: {
            type: 'category',
            data: ['巴西', '印尼', '美国', '印度', '中国', '世界人口(万)']
          },
          series: [
            {
              name: '2011年',
              type: 'bar',
              data: [18203, 23489, 29034, 104970, 131744, 630230]
            },
            {
              name: '2012年',
              type: 'bar',
              data: [19325, 23438, 31000, 121594, 134141, 681807]
            }
          ]
        },
        // 6
        {
          tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
              type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          legend: {
            data: ['直接访问', '邮件营销', '联盟广告', '视频广告', '搜索引擎', '百度', '谷歌', '必应', '其他']
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: [
            {
              type: 'category',
              data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '直接访问',
              type: 'bar',
              data: [320, 332, 301, 334, 390, 330, 320]
            },
            {
              name: '邮件营销',
              type: 'bar',
              stack: '广告',
              data: [120, 132, 101, 134, 90, 230, 210]
            },
            {
              name: '联盟广告',
              type: 'bar',
              stack: '广告',
              data: [220, 182, 191, 234, 290, 330, 310]
            },
            {
              name: '视频广告',
              type: 'bar',
              stack: '广告',
              data: [150, 232, 201, 154, 190, 330, 410]
            },
            {
              name: '搜索引擎',
              type: 'bar',
              data: [862, 1018, 964, 1026, 1679, 1600, 1570],
              markLine: {
                lineStyle: {
                  normal: {
                    type: 'dashed'
                  }
                },
                data: [
                  [{ type: 'min' }, { type: 'max' }]
                ]
              }
            },
            {
              name: '百度',
              type: 'bar',
              barWidth: 5,
              stack: '搜索引擎',
              data: [620, 732, 701, 734, 1090, 1130, 1120]
            },
            {
              name: '谷歌',
              type: 'bar',
              stack: '搜索引擎',
              data: [120, 132, 101, 134, 290, 230, 220]
            },
            {
              name: '必应',
              type: 'bar',
              stack: '搜索引擎',
              data: [60, 72, 71, 74, 190, 130, 110]
            },
            {
              name: '其他',
              type: 'bar',
              stack: '搜索引擎',
              data: [62, 82, 91, 84, 109, 110, 120]
            }
          ]
        }
      ]
      , elemDataView = $('#LAY-index-dataview').children('div')
      , echartsDiv = $('.echartsDiv')
      , randerDivData = function (index) {
        echartsAll[index] = echarts.init(echartsDiv[index], layui.echartsTheme);
        echartsAll[index].setOption(options[index]);
        admin.resize(function () {
          echartsAll[index].resize();
        });
      }
      , renderDataView = function (index) {
        echartsApp[index] = echarts.init(elemDataView[index], layui.echartsTheme);
        echartsApp[index].setOption(options[index]);
        //window.onresize = echartsApp[index].resize;
        admin.resize(function () {
          echartsApp[index].resize();
        });
      };


    //没找到DOM，终止执行
    // if(!elemDataView[0]) return;


    // renderDataView(0);
    for (var i = 0; i < echartsDiv.length; i++) {
      randerDivData(i);
    }

    //监听数据概览轮播
    var carouselIndex = 0;
    // carousel.on('change(LAY-index-dataview)', function(obj){
    //   renderDataView(carouselIndex = obj.index);
    // });

    //监听侧边伸缩
    // layui.admin.on('side', function () {
    //   setTimeout(function () {
    //     renderDataView(carouselIndex);
    //   }, 300);
    // });

    //监听路由
    layui.admin.on('hash(tab)', function () {
      layui.router().path.join('') || renderDataView(carouselIndex);
    });
  });

  //最新订单
  layui.use('table', function () {
    var $ = layui.$
      , table = layui.table;

    //今日热搜
    table.render({
      elem: '#LAY-index-topSearch'
      , url: layui.setter.base + 'json/console/top-search.js' //模拟接口
      , page: true
      , cols: [[
        { type: 'numbers', fixed: 'left' }
        , { field: 'keywords', title: '关键词', minWidth: 300, templet: '<div><a href="https://www.baidu.com/s?wd={{ d.keywords }}" target="_blank" class="layui-table-link">{{ d.keywords }}</div>' }
        , { field: 'frequency', title: '搜索次数', minWidth: 120, sort: true }
        , { field: 'userNums', title: '用户数', sort: true }
      ]]
      , skin: 'line'
    });

    //新闻通告
    table.render({
      elem: '#LAY-index-topCard'
      , url: layui.setter.base + 'json/console/top-card.js' //模拟接口
      , page: false
      , cellMinWidth: 60
      , cols: [[ { field: 'title', title: '标题1', minWidth: 250, templet: '<div><a href="{{ d.href }}" target="_blank" class="layui-table-link">{{ d.title }}</div>' }
        , { field: 'username', title: '发布者' }
        , { field: 'crt', title: '日期', sort: true }
      ]]
    });
  });

  exports('console', {})
});